package com.jz.netthings.bloodsources.service.impl;

import com.jz.netthings.application.bodyCheck.bean.MyPage;
import com.jz.netthings.bloodsources.bean.XyNoPulping;
import com.jz.netthings.bloodsources.bean.XyRemind;
import com.jz.netthings.bloodsources.bean.XyReturnVisit;
import com.jz.netthings.bloodsources.mapper.XyRemindMapper;
import com.jz.netthings.bloodsources.mapper.XyReturnVisitMapper;
import com.jz.netthings.bloodsources.service.XyReturnVisitService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.exception.AdscmSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 回访记录表 服务实现类
 * </p>
 *
 * @author 张凡
 * @since 2019-08-03
 */
@Service
public class XyReturnVisitServiceImpl extends ServiceImpl<XyReturnVisitMapper, XyReturnVisit> implements XyReturnVisitService {
    @Autowired
    XyReturnVisitMapper  xyReturnVisitMapper;
    @Autowired
    XyRemindMapper xyRemindMapper;
    @Override
    public MyPage<XyReturnVisit> queryByCondition(Integer nurseId, String nurseName, String nurseDeptId, String providerName, String createTimeLess, String createTimeGreater,String isComplete, String currentPage, String pageSize) throws AdscmSystemException {
        //XyReturnVisit
        MyPage<XyReturnVisit> myPage=new MyPage<XyReturnVisit>();
        myPage.setCurrentPageIndex(Integer.parseInt(currentPage));
        myPage.setPageSize(Integer.parseInt(pageSize));
        //对当前页参数currentPage 进行重新定义，为节省JVM资源开销，直接将计算后的开始页赋值给参数变量currentPage = beginIndex
        currentPage = myPage.getBeginIndex()+"";
        Integer count = xyReturnVisitMapper.queryByCount(nurseId,nurseName,nurseDeptId,providerName,createTimeLess,createTimeGreater,isComplete);
        List<XyReturnVisit> list  = xyReturnVisitMapper.queryByCondition(nurseId,nurseName,nurseDeptId,providerName,createTimeLess,createTimeGreater,isComplete,Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        //获取相关数据
        myPage.setItems(list);
        myPage.setTotal(count);
        return myPage;
    }
    @Override
    public Boolean updateXyReturnVisitInfo(XyReturnVisit xyReturnVisit)throws AdscmSystemException {
        boolean b = super.updateById(xyReturnVisit);
        //进行提醒消息同步
        new Thread(new SynchronizationXyRemindByReturnVisitRecoredThread(xyReturnVisit)).start();
        return b;
    }

    /**
     * 同步回访信息到最近提醒信息上面
     */
    class  SynchronizationXyRemindByReturnVisitRecoredThread implements  Runnable{
        private XyReturnVisit xyReturnVisit;
        public SynchronizationXyRemindByReturnVisitRecoredThread(XyReturnVisit xyReturnVisit) {
            this.xyReturnVisit = xyReturnVisit;
        }
        public XyReturnVisit getXyReturnVisit() {
            return xyReturnVisit;
        }
        public void setXyReturnVisit(XyReturnVisit xyReturnVisit) {
            this.xyReturnVisit = xyReturnVisit;
        }

        @Override
        public void run() {
            updateXyRemindInfoFromReturnVisitInfo(xyReturnVisit);
        }
    }

    /**
     * 更新提醒信息
     * @return
     */
    public Boolean updateXyRemindInfoFromReturnVisitInfo(XyReturnVisit xyReturnVisit){
        Boolean isSuccess = false;
        if(xyReturnVisit.getCommunicateContent()!=null &&!"".equals(xyReturnVisit.getCommunicateContent())){
            //更新最新的一条提醒消息
            Long providerId =  xyReturnVisit.getProviderId();
            if(providerId!=null && !"".equals(providerId)){
                XyRemind xyRemind = new XyRemind();
                xyRemind.setReturnVisitRecored(xyReturnVisit.getCommunicateContent());
                xyRemind.setRemindProviderId(providerId);
                xyRemindMapper.updateByProviderId(xyRemind);
            }
        }
        return isSuccess;
    }
}
